package com.controller;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.bean.User;
import com.service.UserService;

@SuppressWarnings("serial")
@WebServlet("/RegisterServlet/*")
public class RegisterServlet extends AncestorServlet
{
	
	private final UserService userService = UserService.getInstance();
	
	
	/**
	 * 注册
	 */
	public String signup(HttpServletRequest request, HttpSession session, User user)
	{
		String account = request.getParameter("account");
		String password = request.getParameter("password");
		String cfmpwd = request.getParameter("cfmpwd");
		
		boolean pass = true;
		
		if(userService.existsUserAccount(account))
		{
			pass = false;
			request.setAttribute("account", "此账户名已存在");
		}
		if(!cfmpwd.equals(password))
		{
			pass = false;
			request.setAttribute("cfmpwd", "两次输入的密码不一致");
		}
		
		if(pass)
		{
			userService.insertOneUser(user);
			session.setAttribute("message", "注册成功");
			return "r:/user-login.jsp";
		}
		
		session.setAttribute("message", "注册失败");
		return "f:/user-register.jsp";
	}
	
	
	
	
	
}